package framework.account;

import java.util.Date;

import javax.swing.JOptionPane;

import framework.history.Transaction;
import framework.interfaces.IAccountType;
import framework.interfaces.ICompany;
import framework.interfaces.ITransaction;


public class CompanyAccount extends CustomerAccount {

	public CompanyAccount(IAccountType accountType, ICompany accountHolder) {
		super(accountType, accountHolder);
	}

	@Override
	public void withdraw(double amount) {
		String accNo = accountType.getAccNo();
		double bal = accountType.getBalance();
		ITransaction t = new Transaction(accNo, new Date(), 0, amount);
		accountType.updateBalance(bal - amount);
		accountHistory.addTransactionHistory(t);
		sendNotification(t);
	}

	@Override
	public void deposit(double amount) {
		String accNo = accountType.getAccNo();
		double bal = accountType.getBalance();
		ITransaction t = new Transaction(accNo, new Date(), amount, 0);
		accountType.updateBalance(bal + amount);
		accountHistory.addTransactionHistory(t);
		sendNotification(t);
	}

	@Override
	public void addInterest(double percent) {
		IAccountType acc = getAccountType();
		acc.updateInterest(acc.getInterest() + percent);
	}

	@Override
	public String printStatement(Date begin, Date end) {
		StringBuilder str = new StringBuilder();
		str.append("Company account info ****************************************** :\n");
		str.append(accountHolder + "\n");
		str.append("Account number: " + accountType.getAccNo() + "\n");
		str.append("Account interest: " + accountType.getInterest() + "\n");
		str.append("Current balance: " + accountType.getBalance() + "\n");
		str.append("Transactions ************************************************** :\n");
		for (ITransaction t : accountHistory.getTransactionHistory()) {
			str.append("Date: " + t.getDate());
			str.append(" debit: " + t.getDebitAmount());
			str.append(" credit: " + t.getCreditAmount() + "\n");
		}
		str.append("\n");
		return str.toString();
	}

	@Override
	public void sendNotification(ITransaction t) {
		String msg = "Transaction information: ";
		msg = msg + "\nDate: " + t.getDate().toString();
		msg = msg + "\nCredit amount: " + t.getCreditAmount();
		msg = msg + "\nDebit amount: " + t.getDebitAmount();
		JOptionPane.showMessageDialog(null, msg);
	}

	@Override
	public void charge(double amount) {
		// TODO Auto-generated method stub
	}
}
